package com.google.code.maven.plugin.http.client.transformer;

import java.io.BufferedReader;
import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.maven.plugin.MojoExecutionException;

/**
 * {@link HttpEntity} {@link Transformer} returning the response content as {@link String}
 * 
 * @author Nadim Benabdenbi
 * @version 1.0
 * @since JDK1.6
 * 
 * @see AbstractHttpEntityTransformer
 */
public class HttpEntityContentToString extends AbstractHttpEntityTransformer<String> {
	
	/**
	 * 
	 */
	@Override
	protected String transform(HttpEntity entity) throws MojoExecutionException {
		try {
			StringBuilder result = new StringBuilder();
			BufferedReader reader = getContentReader(entity);
			String line;
			while ((line = reader.readLine()) != null) {
				result.append(line).append("\n");
			}
			return result.toString();
		} catch (IllegalStateException ise) {
			throw new MojoExecutionException("failed to read the http response content", ise);
		} catch (IOException ioe) {
			throw new MojoExecutionException("failed to read the http response content", ioe);
		}
	}
	
}
